package com.mw.service;

import com.mw.domain.News;
import com.mw.repository.NewsRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.List;

@Service
public class NewsService {
    @Autowired
    NewsRepository newsRepository;

    public News getNewsById(int id){
        return newsRepository.findById(id);
    }

    public String getNewsTitleById(int id){
        News news = newsRepository.findById(id);
        return news.getTitle();
    }

    public List<News> findAll(){
        return newsRepository.findAll();
    }

    public List<News> getAllNewsByTypeIsNotNull(){
        return newsRepository.findAllByTypeIsNotNull();
    }

    public List<News> getAllNewsByTypeIsNull(){
        return newsRepository.findAllByTypeIsNull();
    }

    public int saveDifferentNews(News news){
        if(newsRepository.findByTitle(news.getTitle())==null){
            newsRepository.save(news);
            return 1;
        }
        return 0;
    }

    public void updateNews(News news){
        newsRepository.saveAndFlush(news);
    }

    public void delNews(News news){
        newsRepository.delete(news);
    }

    public Page<News> getAllNews(PageRequest pageRequest){

        return newsRepository.findAll(pageRequest);
    }

    /**
     * 分页查询类型不为null的新闻
     * @param page 页码
     * @param size 每页数量
     * @return
     */
    public Page<News> getAllNewsTypeIsNotNull(int page, int size){
        Pageable pageable = new PageRequest(page, size,new Sort(Sort.Direction.DESC,"id"));
        Specification<News> spec = new Specification<News>() {
            public Predicate toPredicate(Root<News> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                Predicate p = cb.isNotNull(root.get("type"));//root相当于sql中的from
                query.where(p);//添加条件
                return query.getRestriction();
            }
        };
        return newsRepository.findAll(spec,pageable);
    }
    /**
     * 分页查询类型为null的新闻
     * @param page 页码
     * @param size 每页数量
     * @return
     */
    public Page<News> getAllNewsTypeIsNull(int page, int size){
        Pageable pageable = new PageRequest(page, size,new Sort(Sort.Direction.DESC,"id"));
        Specification<News> spec = new Specification<News>() {
            public Predicate toPredicate(Root<News> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                Predicate p = cb.isNull(root.get("type"));//root相当于sql中的from
                query.where(p);//添加条件
                return query.getRestriction();
            }
        };
        return newsRepository.findAll(spec,pageable);
    }

    public Page<News> getAllNewsHaveLabel(int page, int size ,String label){
        Pageable pageable = new PageRequest(page, size,new Sort(Sort.Direction.DESC,"id"));
        Specification<News> spec = new Specification<News>() {
            public Predicate toPredicate(Root<News> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                Predicate p = cb.like(root.get("type"),"%"+ label +"%");//root相当于sql中的from
                query.where(p);//添加条件
                return query.getRestriction();
            }
        };
        return newsRepository.findAll(spec,pageable);
    }

}
